<?php //$Rev:: 3          $ // DO NOT REMOVE

error_reporting(E_ALL);

/**
 * Module Tokenizer Class
 *
 * This source code is cover under ATAC's main license,
 * which can be found in the header of the main ATAC.php
 * library file.
 *
 * Please consult the license before distribution
 *
 * $Id: cls_Tokenizer.php 3 2009-09-14 01:07:21Z andrew.heebner $
 * $Author: andrew.heebner $
 */

class Tokenizer
{
    private $file;

    // HOLY HELL, THIS WAS SO PAINSTAKING.  APPRECIATE IT.  PLEASE.  PITY ME TOO.
    public function tokenize_module($file)
    {
        $this->file = file_get_contents($file);
        $tokens = token_get_all($this->file);

        $temp = '';
        $func = array();
        $code = '';
        $i = 0;
        $b = 0;

        if (is_array($tokens)) {
            foreach($tokens as $c) {
                if ($i == 0 && is_array($c) && token_name($c[0]) == 'T_FUNCTION') {
                    $i++;
                }

                if ($i == 1 && is_array($c) && token_name($c[0]) == 'T_STRING') {
                    $tmp = explode('_', $c[1], 2);
                    if (!empty($tmp[1])) {
                        $temp = $tmp[0];
                        $func[$temp]['callback'] = $tmp[1];
                        $i++;
                    } else {
                        $i = 0;
                    }
                }

                if ($i == 2 && $c == '{') {
                    $i++;
                }

                if ($i == 3) {
                    if (is_array($c)) {
                        $code .= $c[1];
                    } else {
                        $code .= $c;
                        if ($c == '{') $b++;
                        if ($c == '}') $b--;
                    }
                    if ($b == 0) {
                        $func[$temp]['code'] = substr_replace(substr_replace($code,'', -1), '',0,1);
                        // important to reset the variables, or you might
                        // end up debugging your code for 5 days *cough*
                        $i = 0; $b = 0; $code = '';
                    }
                }
            }
            return $func;
        }
        return false;
    }

}

?>